import {post} from "./api";
import { onLoad,onShow} from "@dcloudio/uni-app";
import { ref} from "vue";
export const one = (model, param) => {
    let req = {};
    req.model = model;
    req.filters = param||{};
    return post("/data/selectOne", req);
}


export const list = (model, param) => {
    let req = {};
    req.model = model;
    req.filters = param||{};
    return post("/data/list", req);
}


export const loadData = async (model, param, ref) => {
    let res = await one(model, param);
    if (res.code === 200) {
        ref.value = res.data || {};
    }
}
export const loadList = async (model, param, ref) => {
    let res = await list(model, param);
    if (res.code === 200) {
        ref.value = res.data || [];
    }
}

export function useLoadData(model, searchObject) {
    //集合
    let data = ref({})

    onLoad(async () => {
        await refresh();
    })

    const refresh = async () => {
        let res = await one(model, searchObject.value);
        if (res.code !== 200) {
            return;
        }
        data.value = res.data;
    }
    return {
        data,refresh
    }
}


export function useLoadList(model, searchObject) {
    //集合
    let listData = ref([])

    onLoad(async () => {
        await refresh();
    })
    const refresh = async () => {
        let res = await list(model, searchObject.value);
        if (res.code !== 200) {
            return;
        }
        listData.value = res.data;
    }
    return {
        listData,refresh
    }

}


export function useShowData(model, searchObject) {
    //集合
    let data = ref({})

    onShow(async () => {
        await refresh();
    })

    const refresh = async () => {
        let res = await one(model, searchObject.value);
        if (res.code !== 200) {
            return;
        }
        data.value = res.data;
    }
    return {
        data,refresh
    }
}


export function useShowList(model, searchObject) {
    //集合
    let listData = ref([])

    onShow(async () => {
        await refresh();
    })

    const refresh = async () => {
        let res = await list(model, searchObject.value);
        if (res.code !== 200) {
            return;
        }
        listData.value = res.data;
    }

    return {
        listData,refresh
    }
}
